import {defineStore} from 'pinia'
import { reqUserInfo } from '../api/center'
import { removeToken } from '../utils/token';
import router from '../router';
import { showFailToast } from 'vant';

// const router = useRouter()

/* 使用defineStore函数创建一个Store并暴露,接收两个参数,第一个参数是Store名称,相当于模块名,第二个是配置对象,配置State,actions,getters */
export const useUserInfoStore = defineStore('userInfo',{
  state:()=>({
    userInfo:{
      name:'',
      headImg:'',
      nickName:''
    }
  }),
  actions:{
    async getUserInfo(){
      let res = await reqUserInfo()
      if(res.code == 200){
        this.userInfo = res.data
      }else{
        this.resetUserInfo()
        showFailToast('账号登录已过期');
        router.push('/login')
      }
    },

    resetUserInfo(){
      this.userInfo = {
      name:'',
      headImg:'',
      nickName:''
    }
      removeToken()
    }
  },
  getters:{}
})
